package com.sxit.tdf.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxit.tdf.server.entity.FileBucket;
import com.sxit.tdf.server.entity.vo.InsertFileBucketVo;
import com.sxit.tdf.server.mapper.FileBucketMapper;
import com.sxit.tdf.server.service.IFileBucketService;
import com.sxit.tdf.server.util.OSSUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务实现类
 *
 * @author 橙子高
 * @since 2022-02-23
 */
@Service
public class FileBucketServiceImpl extends ServiceImpl<FileBucketMapper, FileBucket>
    implements IFileBucketService {
  @Override
  @Transactional
  public Boolean createBucket(InsertFileBucketVo insertFileBucketVo) {

    FileBucket fileBucket = new FileBucket();
    fileBucket.setBucketName(insertFileBucketVo.getBucketName());
    fileBucket.setBucketCode(insertFileBucketVo.getBucketCode());
    fileBucket.setCreateTime(LocalDateTime.now());

    int insert = baseMapper.insert(fileBucket);
    OSSUtil ossUtil = new OSSUtil();
    ossUtil.createBucket(insertFileBucketVo.getBucketCode());
    return insert == 1;
  }

  @Override
  public void deleteBucket(Integer id) {

    FileBucket fileBucket = baseMapper.selectById(id);

    OSSUtil ossUtil = new OSSUtil();
    ossUtil.deleteBucket(fileBucket.getBucketCode());
    int i = baseMapper.deleteById(id);
  }

  @Override
  public List<FileBucket> getBucketInfo() {
    return baseMapper.getBucketInfo();
  }
}
